﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Text;
using System.Web.Security;
using Newtonsoft.Json;
using MM.Model;
using MM.Common;

namespace WebAPI
{
    /// <summary>
    /// Summary description for TaskAPI
    /// </summary>
    public class TaskAPI : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string GetPostJson = string.Empty;//获取手机post过来的数据
            string ResponseJson = string.Empty;//返回手机数据

            GetPostJson = MM.Common.ComHelper.GetInputStream(context.Request);
            if (!string.IsNullOrEmpty(GetPostJson))
            {
                var Result = JsonConvert.DeserializeObject<JsonResultInfo>(GetPostJson);
                if (Result.info == null)
                {
                    return;
                }
                simInfo sim = Result.info.sim;
                phoneInfo phone = Result.info.phone;
                versionInfo version = Result.info.version;

                MM.LibraryAPI.Common.CheckMobileUser(sim, phone, version);
                List<MM.Model.Rules> Lrules = MM.LibraryAPI.Task.Common.GetRules(sim.imsi.ToStringOr() + phone.mac.ToStringOr(), phone.pid.ToStringOr(), phone.appid.ToStringOr());
                if (Lrules != null && Lrules.Count > 0)
                {
                    MM.Model.Rules mRule = Lrules[0];
                    string strTimer = mRule.Timer.ToStringOr();
                    if (string.IsNullOrEmpty(strTimer))
                        strTimer = MM.LibraryAPI.CommonConfig.Timer;
                    ResponseJson = "{\"control\":{ \"timer\":" + strTimer + ",\"tasktype\":" + mRule.TaskType + "}";

                    List<MM.Model.Task> listTask = mRule.LTask;
                    string signData = string.Empty;
                    if (listTask != null && listTask.Count > 0)
                    {
                        switch (mRule.TaskType)
                        {
                            case "2"://下载安装
                                signData = "\"task\":{\"appurl\":\"" + listTask[0].Value + "\"}";
                                break;
                            case "3"://通知
                                string strUrl = listTask[0].Mark;
                                if (!string.IsNullOrEmpty(strUrl))
                                {
                                    strUrl = MM.LibraryAPI.CommonConfig.NotificationUrl + strUrl;
                                }
                                signData = "\"notification\":{\"title\":\"" + listTask[0].Name + "\",\"msg\":\"" + listTask[0].Value + "\",\"url\":\"" + strUrl + "\"}";
                                break;
                            case "4"://pop框
                                signData = "\"task\":{\"popu\":\"" + listTask[0].Value + "\"}";
                                break;
                            case "5"://启动浏览器
                                signData = "\"task\":{\"browser\":\"" + listTask[0].Value + "\"}";
                                break;
                            case "6"://设置书签
                                string strTemp = string.Empty;
                                foreach (MM.Model.Task mTask in listTask)
                                {
                                    strTemp += "{\"title\":\"" + mTask.Name + "\",\"url\":\"" + mTask.Value + "\"},";
                                }
                                if (!string.IsNullOrEmpty(strTemp))
                                {
                                    strTemp = strTemp.Remove(strTemp.LastIndexOf(','));
                                }
                                signData = "\"task\":{\"bookmark\":[" + strTemp + "]}";
                                break;
                            default:
                                break;
                        }
                    }
                    if (string.IsNullOrEmpty(signData))
                        ResponseJson += "}";
                    else
                        ResponseJson += "," + signData + "}";

                    if (!string.IsNullOrEmpty(ResponseJson))
                    {
                        //ResponseJson = HttpUtility.UrlEncode(ResponseJson, Encoding.GetEncoding("utf-8"));
                        context.Response.Write(ResponseJson);
                    }
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}